package org.example.springioc.DI;

import org.example.springioc.Repository.UserRepository;
import org.example.springioc.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class Controller2 {

    private UserService userService;
    private UserRepository userRepository;

    //默认
    public Controller2() {
    }

    public Controller2(UserService userService) {
        this.userService = userService;
    }
@Autowired
    public Controller2(UserService userService, UserRepository userRepository) {
        this.userService = userService;
        this.userRepository = userRepository;
    }

    public void say(){
        userService.doService();
        userRepository.doRepo();
        System.out.println("hello Controller2");
    }
}

/*       构造函数注入
如果只有一个构造函数，那么会默认调用这个
如果有多个构造函数，且存在无参构造函数，那么会默认调用无参构造函数
如果有多个构造函数，不存在无参构造函数，那么必须使用@Autowired指定

          构造函数规范
一般需要写无参构造函数，使用哪个构造函数，需要加入@Autowired
 */